var fs = require('fs')
var Crawler = require('crawler')
fs.stat('./data',function(err,stats){
    if(err){
        fs.mkdir('./data')
        console.log('创建文件夹成功')
    }
    else{
        console.log('文件夹已存在')
    }
})
var c = new Crawler({
    maxConnections: 10
})

var videoList = [{
    url:'http://www.meipai.com/medias/hot?p=',
    name:'热门',
    code:'remen',
    pageCount:'2'
},{
    url:'http://www.meipai.com/square/6?p=',
    name:'萌宠',
    code:'mengchong',
    pageCount:'2'
}]
videoList.forEach(item=>{
    getVideos(item.code,item.url,item.pageCount)
})

function getVideos(type,url,pageCount){
    getVideoData(url,1,pageCount)
    var videos = []

    function getVideoData(baseUrl,page,pageCount){
        var url = baseUrl + page
        c.queue([
            {
                uri:url,
                callback: function(error,res,done){
                    if(error){
                        console.log(error)
                    }else{
                        var $ = res.$;
                        $('#mediasList li').each(function(){
                            videos.push(convertToVideo($(this)))
                        })
                        if(page <=pageCount){
                            getVideoData(baseUrl,page + 1,pageCount)
                        }
                        else{
                            fs.writeFileSync(`./data/video_${type}.json`, JSON.stringify(videos))
                            console.log(`写入文件完成...video_${type}.json`)
                        }
                    }
                    done();
                }
            }
        ])
    }
}

function convertToVideo(tagVideo){
    var video={}
    video.title = tagVideo.find('.content-l-video a strong').text()
    video.img = tagVideo.find('img').attr('src')
    video.link = tagVideo.find('.content-l-video').attr('data-video')
    video.dataId = tagVideo.find('.content-l-video').attr('data-id')
    return video
}